//
//  TTToStackView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/13.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

enum TTToStackViewType:Int {
    //图片
    case picture = 0
    //@
    case mention = 1
    //#
    case trend = 2
    //表情
    case emoticon = 3
    //+
    case add = 4
}

class TTToStackView: UIStackView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUi()
    }
    
    var btnColse:((TTToStackViewType)->())?
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUi()
    {
        self.axis = .horizontal
        self.distribution = .fillEqually
        addChildButton(image: "compose_toolbar_picture",type: .picture)
        addChildButton(image: "compose_mentionbutton_background",type: .mention)
        addChildButton(image: "compose_trendbutton_background",type: .trend)
        addChildButton(image: "compose_emoticonbutton_background",type: .emoticon)
        addChildButton(image: "compose_add_background",type: .add)
        
    
    }
    
    private func addChildButton(image:String,type:TTToStackViewType)
    {
       let button = UIButton()

        button.addTarget(self, action: #selector(jumto(btn:)), for: .touchUpInside)
        button.tag = type.hashValue
        button.setImage(UIImage(named:image), for: .normal)
        button.setImage(UIImage(named:"\(image)"+"_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        self.addArrangedSubview(button)
 
    }
    @objc private func jumto(btn:UIButton)
    {
        let type = TTToStackViewType(rawValue: btn.tag)
        
        btnColse?(type!)
    
    }
    
}
